package com.essearch.controller;

import com.essearch.entity.*;
import com.essearch.page.Page;
import com.essearch.service.EsSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * elasticsearch 搜索
 */
@RestController
@RequestMapping
@Api(tags = "ES搜索")
public class EsSearchController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private EsSearchService esSearchService;

    /**
     * 新增 / 修改索引
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "新增 / 修改索引")
    @PostMapping("/save")
    public String add(@RequestParam String indexName) {
        return "success";
    }

    /**
     * 删除索引
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "删除索引")
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
        esSearchService.delete(id);
        return "success";
    }
    /**
     * 清空索引
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "清空索引")
    @DeleteMapping("/delete_all")
    public String deleteAll(@PathVariable String id) {
        esSearchService.deleteAll();
        return "success";
    }

    /**
     * 根据ID获取
     * @return
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Object getById(@PathVariable String id){
        return esSearchService.getById(id);
    }

    /**
     * 根据获取全部
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "获取全部")
    @GetMapping("/get_all")
    public List<Object> getAll(){
        return esSearchService.getAll();
    }

    /**
     * 搜索
     * @param keyword
     * @return
     */
    @ApiOperation(value = "搜索")
    @GetMapping("/query/{keyword}")
    public List query(@PathVariable String keyword){
        return esSearchService.query(keyword,
                Item_01.class, Item_01_ext.class, Item_01_list.class, Item_01_list2.class, Item_01_person.class, Item_02.class, Item_02_ext.class, Item_03.class, Item_03_20181222.class, Item_03_ext.class, Item_03_ext_20181222.class, Item_04.class, Item_04_ext.class, Item_05.class, Item_05_20181222.class, Item_05_ext.class, Item_06.class, Item_06_ext.class, Item_06_jfbz.class, Item_06_sbyq.class, Item_06_yjkfhd.class, Item_06_zonghe.class, Item_06_zycp.class, Item_06_zykt.class, Item_07.class, Item_07_ext.class, Item_08.class, Item_08_ext.class, Item_09.class, Item_09_cwzk.class, Item_09_fee.class, Item_09_ryjg.class, Item_09_yjfx.class, Item_10.class, Item_10_ext.class, Item_10_kjjlxm.class, Item_10_lw.class, Item_10_ptjd.class, Item_10_sqxzl.class, Item_10_zykjxm.class, Item_11.class, Item_11_ext.class, Item_11_hzjbqk.class, Item_12.class, Item_12_ext.class, Item_13.class, Item_14.class, Item_14_ext.class, Item_15.class, Item_15_ext.class, Item_16.class, Item_main.class, Item_person.class, Item_person_base.class, Item_person_edu.class, Item_person_ext.class, Item_person_work.class, Item_unit.class, Item_unit_ext.class, Request_person.class, Request_person_ext.class, Request_unit.class, Request_unit_ext.class);
    }

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit_page?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc&pageNo=1&pageSize=1
     * @param pageNo    当前页
     * @param pageSize  每页显示的数据条数
     * @param keyword   关键字
     * @param type    搜索类别
     * @return
     */
    @ApiOperation(value = "搜索，命中关键字高亮(分页)")
    @GetMapping("/query_hit_page")
    public Page<Map<String,Object>> queryHitByPage(@RequestParam int pageNo,@RequestParam int pageSize
                                                    ,@RequestParam String type,@RequestParam String keyword){
        return esSearchService.queryHitByPage(pageNo,pageSize,keyword,type);


    }


    @ApiOperation(value = "保存数据")
    @PostMapping("/saveData")
    public boolean saveData(@RequestParam(value = "object") String object){
        return esSearchService.saveDataToES(object);
    }

    /**
     * 删除索引库
     * @param indexName
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "删除索引库")
    @DeleteMapping("/delete_index/{indexName}")
    public String deleteIndex(@PathVariable String indexName){
        esSearchService.deleteIndex(indexName);
        return "success";
    }
}
